$(function () {
   obj.init();
});
var obj = new function () {
    'use strict';
    return {
        form:null,
        init:function () {
            this.dataGrid = new xhDataGrid($('#attendList'));
            this.depTag = xhTool.getQuery('depTag');
            this.dataGrid.parseToolbar().init(baseUrl+'hrm/attendance/getAttend', this.funObj().getParams()).activeButton([],['bt_list_del']);
            this.dataGrid.command({
                columns:[
                    [
                        {field:'sBaseNumber',title:'钉钉工号',align:'center',halign:'center',rowspan:2,width:120,hidden:true},
                        {field:'sStaffNumber',title:'工号',align:'center',halign:'center',rowspan:2,width:120},
                        // {field:'dayStar',title:'#',align:'center', rowspan:2,halign:'center',width:'20px'},
                        {field:'sStaffName',title:'职工姓名', rowspan:2,align:'center',halign:'center',width:'80px;'},
                        {field:'sDepName',title:'部门',align:'center',halign:'center',rowspan:2,width:120},
                        {title:'考勤(天)', colspan:3,align:'center',halign:'center'},
                        {title:'加班(天)', colspan:3,align:'center',halign:'center'},
                        {title:'旷工(次)', colspan:3,align:'right',halign:'center'},
                        {field:'fixSign',title:'补卡(次)', rowspan:2,align:'right',halign:'center',width:'80px'},
                        {title:'迟到(次)', colspan:2,align:'center',halign:'center'},
                        {title:'早退(次)', colspan:2,align:'center',halign:'center'},
                        {title:'请假(天)', colspan:8,align:'center',halign:'center'}
                    ],
                    [
                        {field:'attendNum',title:'出勤天数',align:'right',halign:'center',width:'80px'},
                        {field:'offDayNum',title:'休息天数',align:'right',halign:'center',width:'80px'},
                        {field:'realAtNum',title:'实际出勤',align:'right',halign:'center',width:'80px'},

                        {field:'overTime',title:'延时',align:'right',halign:'center',width:'80px'},
                        {field:'overWeek',title:'周末',align:'right',halign:'center',width:'80px'},
                        {field:'mealNum',title:'餐补(次)',align:'right',halign:'center',width:'80px'},

                        {field:'morAbsent',title:'上班旷工',align:'right',halign:'center',width:'80px'},
                        {field:'afterAbsent',title:'下班旷工',align:'right',halign:'center',width:'80px'},
                        {field:'absent',title:'缺卡',align:'right',halign:'center',width:'80px'},

                        {field:'late',title:'迟到',align:'right',halign:'center',width:'80px'},
                        {field:'veryLate',title:'严重迟到',align:'right',halign:'center',width:'80px'},

                        {field:'early',title:'早退',align:'right',halign:'center',width:'80px'},
                        {field:'veryEarly',title:'严重早退',align:'right',halign:'center',width:'80px'},

                        {field:'thingOffDay',title:'事假',align:'right',halign:'center',width:'80px'},
                        {field:'offDay',title:'调休',align:'right',halign:'center',width:'80px'},
                        {field:'skillOffDay',title:'病假',align:'right',halign:'center',width:'80px'},
                        {field:'yearOffDay',title:'年假',align:'right',halign:'center',width:'80px'},
                        {field:'loseOffDay',title:'丧假',align:'right',halign:'center',width:'80px'},
                        {field:'compBabyDay',title:'陪产假',align:'right',halign:'center',width:'80px'},
                        {field:'marryOffDay',title:'婚假',align:'right',halign:'center',width:'80px'},
                        {field:'babyOffDay',title:'产假',align:'right',halign:'center',width:'80px'}
                    ]
                ]
            });
            $('#List_Search_DepId').combotree({
                url: baseUrl+'hrm/department/getDepEnum',
                required: false,
                multiple: true,
                cascadeCheck:false
            });
            this.listener();
            this.funObj().bindDgEvent();
        },
        listener:function () {
            var $this = this;
            //导入职工信息
            $('#bt_list_import').click(function () {
                $this.funObj().importAttend();
            });
            //职工搜索
            $('#bt_list_search').click(function () {
                $this.funObj().doSearch();
            });
            xhTool.bindDocKeyDown($('#bt_list_search'));
            //假期设置
            $('#bt_list_holiday').click(function () {
                $this.funObj().setHoliday();
            });

            //加班统计列表
            $('#bt_list_overTimeList').click(function () {
                $this.funObj().openOverTimeList();
            });
            //导出
            $('#excel_export').click(function () {
                $this.funObj().exportExcel();
            });
            //删除
            $('#bt_list_del').click(function () {
                if (xhExtra.buttonDisabled($(this))) {
                    return false;
                }
                $this.funObj().delAttend();
            })
        },
        funObj:function () {
            var $this = this;
            return {
                bindDgEvent:function () {
                     $this.dataGrid.onDblClickRow = function (rowIndex,rowData) {
                         var startTime = xhExtra.getValue($('#List_Search_StartTime'));
                         var endTime = xhExtra.getValue($('#List_Search_EndTime'));
                         var sStaffNumber = rowData['sStaffNumber'];
                         if (!sStaffNumber) {
                             window.top.xhTop.showError('错误','没有工号无法查看其记录');
                             return false;
                         }
                         var url = baseUrl+'hrm/attendance/attendDetail?sStaffNumber='+ sStaffNumber ;
                         url +='&startTime='+startTime;
                         url +='&endTime='+endTime;
                         window.top.xhTop.createNewTab($(rowData['sStaffName']).text()+'-考勤明细', url);
                     };
                    $this.dataGrid.command({
                        rowStyler: function(index,row){
                            if ($(row['sStaffName']).text().indexOf("离职") >= 0 ){
                                return 'background-color:#CC3333;'; // return inline style
                            }
                        }
                    });
                },
                doSearch:function () {
                    $this.dataGrid.reload($this.funObj().getParams());
                },
                importAttend:function () {
                    var htmlStr = '<input id="fileInput" class="swal2-input" type="file" style="width: 100%;"/><br>' ;

                    if ($this.depTag == 'XHCPYFB') {
                        htmlStr += '<select id="moduleType" class="swal2-select" style="width: 100%;">' +
                            '   <option value="moduleOne" selected>模式一(周末4小时半天)[产品研发]</option>' +
                            '</select>';
                    } else if ($this.depTag == 'XHJXB'){
                        htmlStr += '<select id="moduleType" class="swal2-select" style="width: 100%;">' +
                            '   <option value="moduleTwo">模式二(周末按正常工作日)[教学部]</option>' +
                            '</select>';
                    }else {
                        htmlStr += '<select id="moduleType" class="swal2-select" style="width: 100%;">' +
                        '   <option value="moduleOne" selected>模式一(周末4小时半天)[产品研发]</option>' +
                        '   <option value="moduleTwo">模式二(周末按正常工作日)[教学部]</option>' +
                        '</select>';
                    }
                    window.top.swal({
                        title: '导入excel文件',
                        showCancelButton: true,
                        cancelButtonText: '取消',
                        showLoaderOnConfirm: true,
                        allowOutsideClick: true,
                        html:htmlStr,
                        // input: 'file',
                        // inputAttributes: {accept: '.xls,.xlsx'},
                        confirmButtonText: '导入',
                        preConfirm: function() {
                            return new Promise(function (resolve, reject) {
                                var file = window.top.document.getElementById("fileInput").files[0];
                                var moduleType = $('#moduleType', window.top.document).val();
                                if (!file) {
                                    reject('文件不能为空！');
                                } else {
                                    var fileData = new FormData();
                                    fileData.append('file', file);
                                    fileData.append('moduleType', moduleType);
                                    $.ajax({
                                        url: baseUrl + 'hrm/attendance/importAttend',
                                        type: 'POST',
                                        processData: false,
                                        contentType: false,
                                        data: fileData,
                                        complete: function(XMLHttpRequest){
                                            var result = XMLHttpRequest.responseJSON;
                                            if(xhExtra.checkError(result)) return false;
                                            window.top.xhTop.showSuccess(result.msg);
                                            $this.dataGrid.reload();
                                        }
                                    });
                                }
                            })
                        }
                    });
                },
                setHoliday:function () {
                    window.top.xhTop.showModal({
                        title: '假期设置',
                        content: xhTool.getFrameString('hrm/attendance/setHoliday',{}, 600),
                        size: 'md',
                        width: 800,
                        backClose: false
                    });
                },
                openOverTimeList:function () {
                    var startTime = xhExtra.getValue($('#List_Search_StartTime'));
                    var endTime = xhExtra.getValue($('#List_Search_EndTime'));
                    var url = baseUrl+'hrm/attendance/overTimeList?';
                    url += xhTool.parseParam($this.funObj().getParams());
                    window.top.xhTop.createNewTab('加班统计列表', url);
                },
                exportExcel:function () {
                    window.open(baseUrl+'hrm/attendance/exportAttendInfo?'
                        + xhTool.parseParam($this.funObj().getParams()));
                    $this.funObj().doSearch();
                },
                delAttend:function () {
                    var baseNumber = $this.dataGrid.getSelectValue('sBaseNumber');
                    xhExtra.askChangeStatus('hrm/attendance/delAttend', {baseNumber:baseNumber}, $this.dataGrid, '确定要删除此人的考勤记录吗?',function (res) {
                        window.top.xhTop.showSuccess(res.msg);
                    })
                },
                getParams:function () {
                    return {
                        key: xhExtra.getValue($('#list_search')),
                        startTime:xhExtra.getValue($('#List_Search_StartTime')),
                        endTime:xhExtra.getValue($('#List_Search_EndTime')),
                        depTag:$this.depTag
                    }
                }
            }
        }
    }
};